﻿Public Class RendezVous_CTRL
    Implements IDisposable

    Private _listeVeto As List(Of Veterinaires)
    Private _listeClient As List(Of Clients)
    Private _agendaJour As Agendas
    Private _EDM As AniFormEntities

    'Public Event RafraichirLagenda(ByVal sender As Object, ByVal e As EventArgs)

#Region "Cycle de vie du controleur Rendez_vous"

    Public Sub New(ByVal edm As AniFormEntities)
        _EDM = edm
        _listeVeto = New List(Of Veterinaires)
        Dim requete As IEnumerable(Of Veterinaires) = (From unVeto In _EDM.Veterinaires Select unVeto Where unVeto.Archive = False)
        _listeVeto.AddRange(requete.ToList)
        _listeClient = New List(Of Clients)
        Dim requete2 As IEnumerable(Of Clients) = (From unClient In _EDM.Clients Select unClient Where unClient.Archive = False)
        _listeClient.AddRange(requete2.ToList)

    End Sub

    Private disposedValue As Boolean = False        ' Pour détecter les appels redondants

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then

            End If
            For Each element In _listeClient
                element = Nothing
            Next
            For Each element In _listeVeto
                element = Nothing
            Next
            _listeVeto.Clear()
            _listeVeto = Nothing
            _listeClient.Clear()
            _listeClient = Nothing
            _agendaJour = Nothing

        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

#End Region

    Public ReadOnly Property getListeVeto() As List(Of Veterinaires)
        Get
            _listeVeto.Clear()
            Dim requete As IEnumerable(Of Veterinaires) = (From unVeto In _EDM.Veterinaires Select unVeto Where unVeto.Archive = False)
            _listeVeto.AddRange(requete)
            Return _listeVeto.ToList
        End Get
    End Property

    Public ReadOnly Property getListeClient() As IEnumerable(Of Clients)
        Get
            _listeClient.Clear()
            Dim requete As IEnumerable(Of Clients) = (From unClient In _EDM.Clients Select unClient Where unClient.Archive = False)
            _listeClient.AddRange(requete)
            Return _listeClient.ToList
        End Get
    End Property

    Public ReadOnly Property getListeAnimal(ByVal client As Clients) As List(Of Animaux)
        Get
            Dim requete As IEnumerable(Of Animaux) = (From unAnimal In _EDM.Animaux Select unAnimal Where unAnimal.Clients.CodeClient = client.CodeClient And unAnimal.Archive = False)
            Return requete.ToList
        End Get
    End Property

    Public Function getAgendaJour(ByVal veto As Veterinaires, ByVal jour As Date) As List(Of Agendas)
        Dim jourplus1 As Date = DateAdd(DateInterval.Day, 1, jour)
        Dim requete As IEnumerable(Of Agendas) = (From unAgenda In _EDM.Agendas _
                                                  Select unAgenda _
                                                  Where unAgenda.DateRdv > jour.Date _
                                                  And unAgenda.DateRdv < jourplus1.Date _
                                                  And unAgenda.CodeVeto = veto.CodeVeto _
                                                  Order By unAgenda.DateRdv)
        Return requete.ToList
    End Function

    Public Function VerifRdv(ByVal veto As Veterinaires, ByVal creneaurdv As Date) As Boolean
        With creneaurdv
            creneaurdv = New Date(.Year, .Month, .Day, .Hour, .Minute, 0)
        End With
        Dim requete As IEnumerable(Of Agendas) = (From unAgenda In _EDM.Agendas _
                                                  Select unAgenda _
                                                  Where unAgenda.DateRdv = creneaurdv _
                                                  And unAgenda.CodeVeto = veto.CodeVeto)
        If requete.ToList.Count = 0 Then
            Return True
        End If
    End Function


    Public Sub enregistrerRDV(ByVal veto As Veterinaires, ByVal creneaurdv As Date, ByVal client As Clients, ByVal animal As Animaux)
        Dim rdv As Agendas
        rdv = Agendas.CreateAgendas(veto.CodeVeto, creneaurdv, animal.CodeAnimal)
        rdv.Animaux = animal
        rdv.Veterinaires = veto
        _EDM.AddToAgendas(rdv)
        _EDM.SaveChanges(True)

        ''on rafraichi l'affichage de l'agenda -> dans le form
        'RaiseEvent RafraichirLagenda(Me, New EventArgs)
    End Sub

    Public Sub SupprimerRDV(ByVal rdv As Agendas)
        _EDM.DeleteObject(rdv)
        _EDM.SaveChanges(True)
    End Sub

End Class
